package com.yudean.esb.security.impl;

import com.alibaba.fastjson.JSONObject;
import com.yudean.esb.security.ISecurity;
import com.yudean.esb.util.SecurityTools;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author : DL<duanlei@gdyd.com>
 * @version : 1.0
 */
@Service("tokenImpl")
@Scope("prototype")
public class SecurityImpl implements ISecurity {
    @Override
    public String id() {
        String id = SecurityTools.UUID();
        return id;
    }

    @Override
    public String number() {
        String num = SecurityTools.getNumberic();
        return num;
    }

    @Override
    public String verify(String id) {
        HashMap<String, String> result = new HashMap<String, String>();
        if (id == null || "".equals(id.trim())) {
            result.put("error", "1");
            result.put("msg", "token 不能为空");
        } else {
            boolean flag = SecurityTools.verifyUUID(id.trim());
            if (flag) {
                result.put("success", "1");
                result.put("msg", "OK");
            } else {
                result.put("error", "0");
                result.put("msg", "token非法！");
            }
        }
        return JSONObject.toJSONString(result);
    }

    @Override
    public String refresh(String id) {
        HashMap<String, String> result = new HashMap<String, String>();
        if (id == null || "".equals(id.trim())) {
            result.put("success", "1");
            result.put("msg", "token 不能为空");
        } else {
            String token = SecurityTools.refreshToken(id.trim(), 3600);
            result.put("success", "1");
            result.put("token", token);
        }
        return JSONObject.toJSONString(result);
    }
}
